package cn.zh.npro.Fragment;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.shenyuanqing.zxingsimplify.zxing.Activity.CaptureActivity;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.npro.Activity.GoodDtail;
import cn.zh.npro.Activity.GsActivity;
import cn.zh.npro.Adapter.GPadpter;
import cn.zh.npro.Adapter.GdAdapter;
import cn.zh.npro.Adapter.Gpladapter;
import cn.zh.npro.Item.CITem;
import cn.zh.npro.Item.GItem;
import cn.zh.npro.Item.GLItem;
import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import cn.zh.npro.Util.Loading;
import cn.zh.npro.Util.MyListener;
import cn.zh.npro.Util.PullToRefreshLayout;
import cn.zh.npro.Util.PullableListView;
import cn.zh.npro.Util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

import static android.app.Activity.RESULT_OK;

/**
 * Created by WYN on 2018/2/26 0026.
 */

public class GoodsFragment extends Fragment implements View.OnClickListener, PullableListView.OnLoadListener {

    PopupWindow popupWindow;
    ArrayList<GLItem> galist=new ArrayList<GLItem>();
    //当前商品标签
    GLItem glItem=null;
    GPadpter gPadpter;
    TextView gdclass,gttxt;
    RelativeLayout pore;
    //筛选
    //判断弹窗是否可以删除
    boolean isdele=true;
    Gpladapter gpladapter;

    //弹窗队列
    ArrayList<PopupWindow> popus=new ArrayList<PopupWindow>();
    //商品类别数据
    ArrayList<CITem>ciTems=new ArrayList<CITem>();
    //记录返回的商品数据
     CITem fciTem=null;
     ImageView seleimg;
     PopupWindow popupWindowgtype;
     String gdtype="0",flagId="",classId="";
    PullableListView pulist;
    PullToRefreshLayout refresh_view;
    ArrayList<GItem> gItems=new ArrayList<GItem>();
    //暂无数据
    TextView empty,stxt;
    GdAdapter gdAdapter=null;
    int page=1;
    private Dialog LDialog;

    LinearLayout gslin;
    String key="", brand="";
    ImageView scima,wima;
    private static final int REQUEST_SCAN = 6;
    public static final int INIT = 0;
    public static final int STOP = 2;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.gd_layout,container,false);
        initgf(view);
        getgood(page+"");
        return view;
         }


    public void initgf(View view){
        seleimg=view.findViewById(R.id.seleimg);
        scima=view.findViewById(R.id.scima);
        wima=view.findViewById(R.id.wima);
        gslin=view.findViewById(R.id.gslin);
        stxt=view.findViewById(R.id.stxt);
        seleimg.setOnClickListener(this);
        gslin.setOnClickListener(this);
        wima.setOnClickListener(this);
        scima.setOnClickListener(this);
        refresh_view=(PullToRefreshLayout)view.findViewById(R.id.refresh_view);
        empty= (TextView)view.findViewById(R.id.empty);
        pulist= (PullableListView)view.findViewById(R.id.pulist);
        pulist.setOnLoadListener(this);
        gdAdapter=new GdAdapter(getActivity(),gItems);
        pulist.setAdapter(gdAdapter);
        pulist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(getActivity(), GoodDtail.class);
                intent.putExtra("GItem",gItems.get(i));
                startActivity(intent);
                   }
        });
        refresh_view.setOnRefreshListener(new MyListener(){
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                super.onRefresh(pullToRefreshLayout);
               //下拉刷新
                page=1;
                getgood(page+"");
            }
          });
        }


    @Override
    public void onLoad(PullableListView pullableListView) {
        //上拉加载
        page=page+1;
        getgood(page+"");
        }


    //侧滑弹窗
    private void showPopUp() {
        gPadpter = new GPadpter(getActivity(),galist);
        getLabel();
        View vp = LayoutInflater.from(getActivity()).inflate(R.layout.ga_window, null, false);
        gdclass = vp.findViewById(R.id.gdclass);
        gttxt = vp.findViewById(R.id.gttxt);
        if (null!=fciTem){
            gdclass.setText(fciTem.getClassName());
        }else {
            gdclass.setText("全部");
        }
        if (gdtype.equals("0")){
            gttxt.setText("全部");
        }else if (gdtype.equals("1")){
            gttxt.setText("上架");
        }else if (gdtype.equals("2")){
            gttxt.setText("下架");
        }
        LinearLayout gdtype=vp.findViewById(R.id.gdtype);
        GridView gridView=vp.findViewById(R.id.gagrid);
        Button gasure=vp.findViewById(R.id.gasure);
        Button clean=vp.findViewById(R.id.clean);
        View poview=vp.findViewById(R.id.poview);
        final LinearLayout gachose=vp.findViewById(R.id.gachose);
        pore = vp.findViewById(R.id.pore);
        gasure.setOnClickListener(this);
        clean.setOnClickListener(this);
        gachose.setOnClickListener(this);
        gdtype.setOnClickListener(this);
        poview.setOnClickListener(this);
        gridView.setAdapter(gPadpter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (int j=0;j<galist.size();j++){
                    if (j==i){
                        galist.get(j).setType(1);
                        glItem=galist.get(j);
                    }else {
                        galist.get(j).setType(0);
                    }
                }
                gPadpter.notifyDataSetChanged();
             }
          });

        WindowManager wm = (WindowManager) getActivity()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int height = outMetrics.heightPixels;

        popupWindow = new PopupWindow(vp, LinearLayout.LayoutParams.MATCH_PARENT, height);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.getContentView().setFocusableInTouchMode(true);
        popupWindow.getContentView().setFocusable(true);
        popupWindow.setClippingEnabled(false);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        popupWindow.showAtLocation(getActivity().findViewById(R.id.gd_layout), Gravity.NO_GRAVITY, 0, 0);
        }



    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
            case R.id.seleimg:
                showPopUp();
                break;
            case R.id.gachose:
                getPo();
                getclss();
                break;
            case R.id.gdtype:
                typePo();
                break;
            case R.id.gall:
                popupWindowgtype.dismiss();
                gdtype="0";
                gttxt.setText("全部");
                break;
            case R.id.gup:
                popupWindowgtype.dismiss();
                gdtype="1";
                gttxt.setText("上架");
                break;
            case R.id.gdown:
                popupWindowgtype.dismiss();
                gdtype="2";
                gttxt.setText("下架");
                break;
            case R.id.gasure:
                page=1;
                String str="";
                if (null==gdclass){
                    str="全部";
                  }else {
                    str = gdclass.getText().toString();
                }
                if (str.equals("全部")){
                    glItem=null;
                    fciTem=null;
                   }
                popupWindow.dismiss();

                //此处加需判断
                getgood(page+"");

                break;
            case R.id.clean:
                page=1;
                gdclass.setText("全部");
                for (int j=0;j<galist.size();j++){
                    galist.get(j).setType(0);
                  }
                gPadpter.notifyDataSetChanged();
                fciTem=null;
                glItem=null;
                popupWindow.dismiss();
                getgood(page+"");
                break;
            case R.id.gslin:
                Intent intent1=new Intent(getActivity(),GsActivity.class);
                intent1.putExtra("type",0);
                startActivityForResult(intent1,0);
                break;
            case R.id.wima:
                stxt.setText("客户名称");
                key="";
                page=1;
                getgood(page+"");
                wima.setVisibility(View.GONE);
                break;
            case R.id.scima:
                getRuntimeRight();
                break;
            case R.id.poview:
                popupWindow.dismiss();
                break;
              }
           }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case 2:
                String name=data.getStringExtra("name");
                key=name;
                stxt.setText(name);
                page=1;
                getgood(page+"");
                wima.setVisibility(View.VISIBLE);
                break;
            }

        if(requestCode == REQUEST_SCAN && resultCode == RESULT_OK){
            page=1;
            String str="";
            if (null==gdclass){
                str="全部";
            }else {
                str = gdclass.getText().toString();
              }
            if (str.equals("全部")){
                glItem=null;
                fciTem=null;
             }

            key=data.getStringExtra("barCode");
            //此处加需判断
            getgood(page+"");
            }
          }


    public void getPo(){
        isdele=true;
        View vpa = LayoutInflater.from(getActivity()).inflate(R.layout.gaa_layout, null, false);
        WindowManager wm = (WindowManager) getActivity()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int  mScreenWitdh = outMetrics.widthPixels;
        int lweith=mScreenWitdh/5;

        final ImageView gaa=vpa.findViewById(R.id.gaa_back);
        ListView gaa_list=vpa.findViewById(R.id.gaa_list);
        gpladapter = new Gpladapter(getActivity(),ciTems);
        gaa_list.setAdapter(gpladapter);

        gaa_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (null==ciTems.get(i).getChildClass()||ciTems.get(i).getChildClass().length()==0){
                    fciTem=ciTems.get(i);
                    gdclass.setText(fciTem.getClassName());
                    popus.get(0).dismiss();
                 }else {
                    getPodata(ciTems.get(i).getChildClass());
                }
              }
           });

        final PopupWindow popupWindowga = new PopupWindow(vpa, mScreenWitdh-lweith, LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindowga.setFocusable(true);
        popupWindowga.setOutsideTouchable(true);
        popupWindowga.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindowga.getContentView().setFocusableInTouchMode(true);
        popupWindowga.getContentView().setFocusable(true);
        popupWindowga.setAnimationStyle(R.style.popwin_anim_style);
        gaa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindowga.dismiss();
            }
        });

        popupWindowga.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (isdele) {
                    if (popus.size()>0) {
                        popus.remove((popus.size() - 1));
                    }
                }
            }
        });

        popus.add(popupWindowga);
        popupWindowga.showAtLocation(getActivity().findViewById(R.id.gd_layout), Gravity.NO_GRAVITY, lweith, 0);
        }




    public void typePo(){
        View vpa = LayoutInflater.from(getActivity()).inflate(R.layout.gtype, null, false);
        WindowManager wm = (WindowManager) getActivity()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int  mScreenWitdh = outMetrics.widthPixels;
        int lweith=mScreenWitdh/5;
        final ImageView gaa=vpa.findViewById(R.id.gty_back);
         ImageView duione=vpa.findViewById(R.id.duione);
         ImageView duitwo=vpa.findViewById(R.id.duitwo);
         ImageView duithree=vpa.findViewById(R.id.duithree);
         duione.setVisibility(View.GONE);
         duitwo.setVisibility(View.GONE);
         duithree.setVisibility(View.GONE);
         switch (gdtype){
             case "0":
                 duione.setVisibility(View.VISIBLE);
                 break;
             case "1":
                 duitwo.setVisibility(View.VISIBLE);
                 break;
             case "2":
                 duithree.setVisibility(View.VISIBLE);
                 break;
              }
        LinearLayout gall=vpa.findViewById(R.id.gall);
        LinearLayout gup=vpa.findViewById(R.id.gup);
        LinearLayout gdown=vpa.findViewById(R.id.gdown);
        gall.setOnClickListener(this);
        gup.setOnClickListener(this);
        gdown.setOnClickListener(this);

        popupWindowgtype = new PopupWindow(vpa, mScreenWitdh-lweith, LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindowgtype.setFocusable(true);
        popupWindowgtype.setOutsideTouchable(true);
        popupWindowgtype.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindowgtype.getContentView().setFocusableInTouchMode(true);
        popupWindowgtype.getContentView().setFocusable(true);
        popupWindowgtype.setAnimationStyle(R.style.popwin_anim_style);
        gaa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindowgtype.dismiss();
            }
        });

        popupWindowgtype.showAtLocation(getActivity().findViewById(R.id.gd_layout), Gravity.NO_GRAVITY, lweith, 0);
        }



    //网络请求
    //获取商品标签
    public  void getLabel(){
        galist.clear();
        OkHttpUtils.get(BaseUrl.GetGoodsLabel)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean status=js.getBoolean("State");
                            if (status){
                                JSONArray ja=js.getJSONArray("Res");
                                for (int i=0;i<ja.length();i++){
                                    JSONObject ji=ja.getJSONObject(i);
                                    String FlagID=ji.getString("FlagID");
                                    String FlagType=ji.getString("FlagType");
                                    String FlagName=ji.getString("FlagName");
                                    String BGColor=ji.getString("BGColor");
                                    String FontColor=ji.getString("FontColor");
                                    GLItem glItem=new GLItem(0,FlagID ,FlagType,FlagName,BGColor,FontColor);
                                    galist.add(glItem);
                                }
                                gPadpter.notifyDataSetChanged();
                            }else {
                                String msg=js.getString("msg");
                                ToastUtil.showLong(getActivity(),msg);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                         }
                      }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
                }

    //获取商品类别请求
    public void getclss(){
        MyApplication mapp= (MyApplication)getActivity(). getApplication();
        SharedPreferences sp = mapp.getSp();
        OkHttpUtils.get(BaseUrl.getGoodsClass+"?UserId="+ sp.getString("uid",""))
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean state=js.getBoolean("State");
                            if (state){
                                ciTems.clear();
                                JSONArray ja=js.getJSONArray("Res");
                                CITem ciTem0=new CITem("","全部","","",null);
                                ciTems.add(ciTem0);
                                for (int i=0;i<ja.length();i++){
                                    JSONObject ji=ja.getJSONObject(i);
                                    String ClassID=ji.getString("ClassID");
                                    String ClassName=ji.getString("ClassName");
                                    String FatherId=ji.getString("FatherId");
                                    String ClassID2=ji.getString("ClassID2");
                                    JSONArray ChildClass=ji.getJSONArray("ChildClass");
                                    CITem ciTem=new CITem(ClassID,ClassName,FatherId,ClassID2,ChildClass);
                                    ciTems.add(ciTem);
                                }
                                gpladapter.notifyDataSetChanged();

                            }else {
                                String Msg=js.getString("Msg");
                                ToastUtil.showLong(getActivity(),Msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
           }




    public void getPodata(JSONArray ja){
        final ArrayList<CITem> arrciTems=new ArrayList<CITem>();
        for (int i=0;i<ja.length();i++){
            try {
                JSONObject ji=ja.getJSONObject(i);
                String ClassID=ji.getString("ClassID");
                String ClassName=ji.getString("ClassName");
                String FatherId=ji.getString("FatherId");
                String ClassID2=ji.getString("ClassID2");
                JSONArray ChildClass=ji.getJSONArray("ChildClass");
                CITem ciTem=new CITem(ClassID,ClassName,FatherId,ClassID2,ChildClass);
                arrciTems.add(ciTem);
            }catch (JSONException e) {
                e.printStackTrace();
            }
        }
        View vpa = LayoutInflater.from(getActivity()).inflate(R.layout.gaa_layout, null, false);
        WindowManager wm = (WindowManager) getActivity()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int  mScreenWitdh = outMetrics.widthPixels;
        int lweith=mScreenWitdh/5;
        ImageView gaa=vpa.findViewById(R.id.gaa_back);
        ListView gaa_list=vpa.findViewById(R.id.gaa_list);

        Gpladapter gpladapter = new Gpladapter(getActivity(),arrciTems);
        gaa_list.setAdapter(gpladapter);
        gaa_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (arrciTems.get(i).getChildClass().length()==0){
                    fciTem=arrciTems.get(i);
                    gdclass.setText(fciTem.getClassName());
                    isdele=false;
                    for (int j=0;j<popus.size();j++){
                        popus.get(j).dismiss();
                    }
                    popus.clear();
                }else {
                    getPodata(arrciTems.get(i).getChildClass());
                }
              }
        });
        final PopupWindow popupWindowga = new PopupWindow(vpa, mScreenWitdh-lweith, LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindowga.setFocusable(true);
        popupWindowga.setOutsideTouchable(true);
        popupWindowga.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindowga.getContentView().setFocusableInTouchMode(true);
        popupWindowga.getContentView().setFocusable(true);
        popupWindowga.setAnimationStyle(R.style.popwin_anim_style);

        gaa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindowga.dismiss();
            }
        });
        popupWindowga.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (isdele) {
                    popus.remove((popus.size() - 1));
                }
              }
        });
        popus.add(popupWindowga);
        popupWindowga.showAtLocation(getActivity().findViewById(R.id.gd_layout), Gravity.NO_GRAVITY, lweith, 0);
          }


        public void getgood(final String page){
            if (null!=glItem){
                flagId=glItem.getFlagID();
            }else {
                flagId="";
            }
            if (null!=fciTem){
                classId=fciTem.getClassID();
            }else {
                classId="";
            }
        if (page.equals("1")){
            LDialog= Loading.createLoadingDialog(getActivity(),"加载中...");
           }
            MyApplication mapp= (MyApplication)getActivity(). getApplication();
            SharedPreferences sp = mapp.getSp();
            OkHttpUtils.get(BaseUrl.getGoodsPlus+"?UserId="+ sp.getString("uid",""))
                    .params("page",page+"")
                    .params("size","7")
                    .params("key",key)
                    .params("flagId",flagId)
                    .params("brand",brand)
                    .params("bBlockUp",gdtype)
                    .params("classId",classId)
                    .execute(new StringCallback() {
                        @Override
                        public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                            System.out.println("--------->商品列表==="+s);
                            if (null!=LDialog){
                                Loading.closeDialog(LDialog);
                             }
                            try {
                                JSONObject js=new JSONObject(s);
                                boolean State=js.getBoolean("State");
                                if (State) {
                                    JSONArray Res = js.getJSONArray("Res");
                                    if (page.equals("1")){
                                        gItems.clear();
                                        if (0==Res.length()){
                                            refresh_view.setVisibility(View.INVISIBLE);
                                            empty.setVisibility(View.VISIBLE);
                                            pulist.finishLoading(INIT);
                                            return;
                                        }else {
                                            refresh_view.setVisibility(View.VISIBLE);
                                            empty.setVisibility(View.GONE);
                                        }
                                      }else {
                                        if (Res.length()==0){
                                            pulist.finishLoading(STOP);
                                            return;
                                          }
                                       }
                                    for (int i = 0; i < Res.length(); i++) {
                                        JSONObject ji = Res.getJSONObject(i);
                                        String GoodsId = ji.getString("GoodsId");
                                        String GoodsName = ji.getString("GoodsName");
                                        String GoodsNo = ji.getString("GoodsNo");
                                        String IndexURL = ji.getString("IndexURL");
                                        String Price_Detail = ji.getString("Price_Detail");
                                        String Unit = ji.getString("Unit");
                                        GItem gItem = new GItem(GoodsId, GoodsName, GoodsNo, IndexURL, Price_Detail, Unit);
                                        gItems.add(gItem);
                                       }
                                    gdAdapter.notifyDataSetChanged();
                                    pulist.finishLoading(INIT);
                                }else {
                                    String msg=js.getString("msg");
                                    ToastUtil.showLong(getActivity(),msg);
                                    if (page.equals("1")){
                                        pulist.finishLoading(INIT);
                                    }else {
                                        pulist.finishLoading(STOP);
                                    }
                                    refresh_view.setVisibility(View.INVISIBLE);
                                    empty.setVisibility(View.VISIBLE);
                                }
                             } catch (JSONException e) {
                                e.printStackTrace();
                              }
                           }
                        @Override
                        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onError(isFromCache, call, response, e);
                        }
                    });
              }


    /**
     * 获得运行时权限
     */
    private void getRuntimeRight() {
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 1);
        } else {
            jumpScanPage();
        }
       }


    /**
     * 跳转到扫码页
     */
    private void jumpScanPage() {
        startActivityForResult(new Intent(getActivity(), CaptureActivity.class),REQUEST_SCAN);
      }

    /**
     * 获取状态通知栏高度
     * @param activity
     * @return
     */
    public static int getStatusBarHeight(Activity activity) {
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        return frame.top;
    }

}
